package cn.iocoder.yudao.module.fm.utils;

import lombok.SneakyThrows;
import org.apache.poi.util.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;

/**
 * @author SUN
 * @date 2023/11/19
 */
public class DownloadTemplateUtil {


    @SneakyThrows
    public static ResponseEntity<byte[]> downloadTemplate(String fileName, String templateName) {
        ClassPathResource classPathResource = new ClassPathResource(templateName);
        byte[] byteArray = IOUtils.toByteArray(classPathResource.getInputStream());

        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", "attachment; filename=\"" + fileName + "\"; filename*=utf-8''" + fileName);
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        headers.add("Content-Type", "application/vnd.ms-excel;charset=UTF-8");
        return ResponseEntity.ok().headers(headers).body(byteArray);
    }

}
